package com.haifeng.mywriteaidldemo;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;

import com.haifeng.mywriteaidldemo.MyAIDL.MyInterface;
import com.haifeng.mywriteaidldemo.MyAIDL.Proxy;
import com.haifeng.mywriteaidldemo.MyAIDL.Stub;

//链接器
public class MyServiceConnection implements ServiceConnection {


    MyInterface interfacs;

    public static MyServiceConnection instance;

    public static MyServiceConnection getInstance() {
        if (instance==null){
            synchronized (MyServiceConnection.class){
                instance=new MyServiceConnection();
            }
        }
        return instance;
    }

    //启动远程服务
    public static void BindService(){
        Intent intent=new Intent(MyApp.getInstance(),MyService.class);
        MyApp.getInstance().bindService(intent,getInstance(),Context.BIND_AUTO_CREATE);
    }

    public MyInterface getInterfacs() {
        return interfacs;
    }

    @Override
    public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
        System.out.println("the service started the interfacs get value");
        //通过binder返回代理类   从而对远程方法进行数据操作
        interfacs= Stub.anInterface(iBinder);
    }

    @Override
    public void onServiceDisconnected(ComponentName componentName) {
        interfacs=null;
        BindService();
    }
}
